var utils = require('../../../utils/util.js');
var c = require('../../../utils/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankList: [],
    start:0,
    end:0,
    Leftdraw:''
  },
  onLoad: function (options) {
    this.getBanklist()
  },
  onShow: function () {
    utils.checkLogin();
  },
  //下拉刷新
  onPullDownRefresh: function () {
    this.setData({
      bankList: []
    });
    this.getBanklist();
  },
  getBanklist: function () {
    var that = this;
    var token = wx.getStorageSync("userinfo").wap_token;
    var data = {
      url: "/v1/userbank/lists",
      token: token,
      type:'',
    };
    utils.request(data).then(function (res) {
        if (res.code == 0) {
          var data = res.data
            data.forEach((item, index) => {
                var str = item.bank_no;
                data[index]['bank_no'] = str.substr(str.length-4);
                data[index]['img_info']['background_pic'] = c.img_url+item.img_info.background_pic;
                data[index]['Leftdraw'] = 0;
            });
            that.setData({
                bankList:data
            });
        }
        wx.stopPullDownRefresh();
    });
  },
  setDefault:function(e){
        var that = this;
        var bankid = e.currentTarget.dataset.bankid;
        wx.showModal({
            title: '提示',
            content: '设为默认',
            success: function(res) {
                if (res.confirm) {
                    var token = wx.getStorageSync("userinfo").wap_token;
                    var data = {
                      url: "/v1/userbank/default",
                      token: token,
                      id:bankid
                    };
                    utils.request(data).then(function (res) {
                        if (res.code == 0) {
                            wx.showToast({
                                title: '设置默认成功',
                                icon: 'none',
                                duration: 1500
                            });
                            setTimeout(function(){
                              that.getBanklist()
                            },1000)
                        }else{
                            wx.showToast({
                                title: res.msg,
                                icon: 'none',
                                duration: 1500
                            }) 
                        }
                    });
                } else if (res.cancel) {
                  
                }
            }
        })
    },
    //删除银行卡
    delete:function(e){
        var that = this;
        var bankid = e.currentTarget.dataset.bankid;
        var token = wx.getStorageSync("userinfo").wap_token;
        var data = {
          url: "/v1/userbank/del",
          token: token,
          id:bankid
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                wx.showToast({
                    title: '删除成功',
                    icon: 'none',
                    duration: 1500
                });
                setTimeout(function(){
                  that.getBanklist()
                },1000)
            }else{
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1500
                }) 
            }
        });
    },
  starttouch:function(e){
    this.setData({
      start: e.changedTouches[0].clientX
    })
  },
  endtouch: function (e) {
    let i = e.currentTarget.dataset.index;
    let start = this.data.start;
    let end = e.changedTouches[0].clientX;
    let str = "bankList[" + i +"].Leftdraw"
    if ( start > end ){
      this.setData({
        [str]:-180
      })
    }else{
      this.setData({
        [str]: 0
      })
    }
  }
})